﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class Contact
    {
        public ConnectGetData _connectData;
        public Contact()
        {
            _connectData = new ConnectGetData();
         }

        /// <summary>
        /// create comment
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="memberID"></param>
        /// <param name="contentComment"></param>
        /// <returns> true if success</returns>
        public bool ContactCreate(String fullname, String email, String phone, String content)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sqlUp = "Insert into Contact (FullName, Email, Phone, Content, IsAnswer) values(@FullName, @Email, @Phone, @Content,0)";
                SqlCommand cmd = new SqlCommand(sqlUp, con);

                cmd.Parameters.AddWithValue("@FullName", fullname);
                cmd.Parameters.AddWithValue("@Email", email);
                cmd.Parameters.AddWithValue("@Phone", phone);
                cmd.Parameters.AddWithValue("@Content", content);
                
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// get all album for admin or user
        /// </summary>
        /// <returns>datatable</returns>
        public DataTable GetContact()
        {
            String str = "select  * from Contact ORDER BY ContactID DESC";
            return _connectData.GetDataTable(str);
        }

        /// <summary>
        /// delete Contact by ID 
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public bool ContactDelete(String contacttID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "delete from Contact  where ContactID = @ContactID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ContactID", contacttID);

                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// update contact status : enable or disable
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="status"></param>
        public void ChangeContactStatus(String contactID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            string sql = "update Contact set IsAnswer = 'True' where ContactID = @ContactID";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@ContactID", contactID);

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

        /// <summary>
        /// get total number of unanswered contact of kids for his/her parent
        /// </summary>
        /// <param name="articleTypeID"></param>
        /// <returns>int</returns>
        public global::System.Nullable<int> TotalUnaswerOfContact()
        {

            SqlConnection con = _connectData.GetConnect();
            con.Open();
            string sql = "select  COUNT(ContactID) from  Contact  where  IsAnswer = 0";
            SqlCommand cmd = new SqlCommand(sql, con);

            object returnValue;

            returnValue = cmd.ExecuteScalar();
            con.Close();
            if (((returnValue == null) || (returnValue.GetType() == typeof(global::System.DBNull))))
            {
                return new global::System.Nullable<int>();
            }
            else
            {
                return new global::System.Nullable<int>(((int)(returnValue)));
            }
        }
    }
}